package cn.tank.rabbitwork.controller;

import cn.tank.rabbitwork.entity.Order;
import cn.tank.rabbitwork.service.IMQService;
import cn.tank.rabbitwork.service.IOrderService;
import cn.tank.rabbitwork.service.IPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IMQService mqService;
    @Autowired
    private IPayService payService;
    @Autowired
    private IOrderService orderService;

    @RequestMapping("/addview")
    public String addView(){
        return "addView";
    }

    @RequestMapping("/listorder")
    public String listOrder(Model model){
        List<Order> rsList = orderService.QueryOrder("张三");
        if(rsList!=null && rsList.size() > 0){
            model.addAttribute("psnName",rsList.get(0).getPsnName());
            model.addAttribute("rsList",rsList);
        }

        return "listOrder";
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(Order order){
        try {
            mqService.sendNewOrderMsg(order);
        }catch (Exception e){
            return e.getMessage();
        }

        return "redirect:listorder";
    }

    @RequestMapping(value = "/pay",method = RequestMethod.POST)
    public String pay(Integer orderId){
        try{
            payService.payOrder(orderId);
        }catch (Exception e){
            return e.getMessage();
        }

        return "redirect:listorder";
    }




}
